//
//  QJShopDetailTwoVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/1/31.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJShopDetailTwoVC.h"
#import "QJDetailVCRightImageCell.h"
#import "SDImageCache.h"

@interface QJShopDetailTwoVC () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *twoTableView;
@property (nonatomic, strong) NSMutableArray *imageArray;
@end

@implementation QJShopDetailTwoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.imageArray = [NSMutableArray array];
    self.twoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT - Height_NavBar - 70) style:UITableViewStylePlain];
    [self.view addSubview:self.twoTableView];
    self.twoTableView.delegate = self;
    self.twoTableView.dataSource = self;
    self.twoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册cell
    [self.twoTableView registerNib:[UINib nibWithNibName:@"QJDetailVCRightImageCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    // 缓存大小 测试
    NSUInteger tmpSize = [[SDImageCache sharedImageCache] getSize];
    NSUInteger count = [[SDImageCache sharedImageCache] getDiskCount];
    //    KMyLog(@"tmpSize = %f, count = %zd", tmpSize/(1024.f*1024.f), count);
    // 测试结束
}
- (void)detailTwoUpdateWithDic:(NSMutableDictionary *)dict{
    NSDictionary *dic = (NSDictionary *)dict[@"data"];
    NSArray *imageArray = [dic objectForKey:@"goods_images_list"];
    for (int i = 0; i < imageArray.count; i++) {
        NSString *imageUrl = [NSString stringWithFormat:@"%@",[imageArray[i]objectForKey:@"image_url"]];
        
        [self.imageArray addObject:imageUrl];
        
    }
    
    [self.twoTableView reloadData];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.imageArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    KMyLog(@"cellForRowAtIndexPath");
    
    QJDetailVCRightImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellUpdateWithImageUrl:self.imageArray[indexPath.row]];
    cell.updateCellHeightBlock = ^{ // 估算值跟实际值有差距, 这里scrollview会跳动, 而且cell会错位
        //刷新单个cell
        //        KMyLog(@"刷新==============单个cell");
        NSIndexPath *indexPathA = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathA,nil] withRowAnimation:UITableViewRowAnimationNone];
    };
    return cell;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *imgURL = self.imageArray.count > indexPath.row ? self.imageArray[indexPath.row] :nil;
    if (imgURL) {
        //根据当前Row的ImageUrl作为Key获取图片缓存
        UIImage *img = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgURL];
        if (!img) {
            //            img = [UIImage resizedImageWithName:@"childshow_placeholder"];
            img = [UIImage imageNamed:@"noImage"];
            
        }
        CGFloat height = img.size.height *UISCREEN_WIDTH/img.size.width;//Image宽度为屏幕宽度 ，计算宽高比求得对应的高度
        //        KMyLog(@"----------------return Height:%f",height);
        return height;
        
    }
    return 0;
    //    QJDetailVCRightImageCell *cell = (QJDetailVCRightImageCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    //    return cell.cellHeight;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //解决内存泄漏   点击"加入购物车",再点击"详情页",没有数据bug
    //    self.twoTableView.dataSource = nil;
    //    self.twoTableView.delegate = nil;
    //    [self.twoTableView removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

